home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3dkit1.zip / 3DHELP.C < prev    next >
C/C++ Source or Header  |  1992-05-18  |  10KB  |  253 lines

  1. /*   (C source follows this text)
  2.  
  3. Date: 22 Sep 91 09:41:53 EDT
  4. From: Bruce Colletti <71121.1452@CompuServe.COM>
  5. Subject: 3D Viewer
  6. To: Oscar Garcia <garciao@MOF.GOVT.NZ>
  7. Message-Id: <910922134153_71121.1452_DHJ35-1@CompuServe.COM>
  8.  
  9. Hi again Oscar.  I liked your 3D Viewer 2.5 so much that I created the
  10. following program to construct the input *.3d files.  My program is
  11. being posted to BBS' I find that have your Viewer.  Per your written
  12. instructions, I am not modifying your original distribution file to
  13. include mine.  However, the following program may be included in your
  14. distribution package if you so wish.  The complete program is being
  15. sent in two different Emails (CompuServe has a limit to the size of
  16. messages).
  17.  
  18. Again, thanx for distributing 3D Viewer.  Not only is it fun to use,
  19. but can be used in applications and in teaching geometry to students.
  20.  
  21.  
  22. Bruce Colletti (71121.1452@compuserve.com); 22 SEP 91 0941 EST
  23.         ---------------------- cut here ----------------------
  24. public domain, three dimensional surface builder called 3-D Viewer
  25. v2.5.  This lets you rotate 3D surfaces using a Microsoft compatible
  26. mouse.  Two public BBS' where it can be found (3DV25.ZIP) are
  27. ShadeTree (412-244-9416 2400N81, Pittsburgh Pennsylvania, USA) and the
  28. Blue Ridge Express (804-790-1675 2400N81, Richmond Virginia, USA).  I
  29. built the program below (3DHELP) for my own use:  it builds the *.3D
  30. input file to 3-D Viewer for the equation which the user specifies in
  31. the procedure f.
  32.  
  33. This is how 3DHELP works:  the XY domain is defined by a rectangle
  34. whose lower left and upper right hand corners are (xlo,ylo) and (xhi,yhi)
  35. respectively.  This "rubber sheet" is then distorted:  each (x,y)
  36. point is "stretched" vertically by its z value.  Although this is a
  37. useful way to specify the XY domain, it prevents 3DHELP from plotting
  38. general surfaces:  the domain of a hemisphere is a disk and not a
  39. rectangle.  Maybe you can change 3DHELP to make it plot more general
  40. surfaces.
  41.  
  42. The XYZ axes are displayed in the colors red, white, and blue
  43. respectively. Each axis extends from the min/max values assumed by the
  44. points.
  45.  
  46. One output file (specify a *.3D extension!) has the points to plot.  A
  47. second output file connects the XY points in a grid.  The way that
  48. these files are formatted is described in Oscar's 3DV.DOC.  Once all
  49. points have been determined, the second file is appended (to the
  50. first) and then erased.  The resulting *.3D file can then be used by
  51. Oscar's 3DV.EXE.
  52.  
  53. This program was created using TURBO C 2.01 w/patches ad entered into
  54. the public domain.  No guarantee is made about 3DHELP's suitability
  55. for use, which is at your own risk.  This is source code which you can
  56. modify to your heart's content.  I haven't tried to make this code
  57. portable, so it may need to be changed to compile correctly in your C
  58. compiler.  Be aware that this program creates the file ###.### on the
  59. current directory and then erases it later, and doesn't check if the
  60. user-specified *.3d file already exists.
  61.  
  62. 3DHELP.C may be freely distributed but without charge.
  63.  
  64. Bruce W. Colletti
  65. Richmond VA
  66. 71121.1452@compuserve.com
  67. 21AUG91
  68. */
  69.  
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <math.h>
  73.  
  74.  *******************************************************************
  75.  This is the function to display in 3 dimensional space, i.e., plot
  76.  the points {(x,y,z) : z = f(x,y)}
  77.  
  78.  The example function is 3sin(x}+y}sin(10y)).
  79.  *******************************************************************
  80.  ******************************************************************/
  81.  
  82. float f(float x, float y)
  83.   {float z;
  84.    z = 3*sin(x*x + sin(10*y)*y*y);
  85.    return(z);
  86.   }
  87.  
  88.  ****************************** end procedure f **********************
  89.  ********************************************************************/
  90.  
  91.  ********************** read a real number **************************
  92.  ********************************************************************/
  93.  
  94.   {char buffer[10];
  95.  
  96.    while (gets(buffer) && (sscanf(buffer,"%f",x)==0))
  97.      printf("Numeric input only.  Correct value /");
  98.  
  99.    return;
  100.   }
  101.  
  102.  ********************** read an integer    **************************
  103.  *******************************************************************/
  104.  
  105. void readint(int *x)
  106.   {char buffer[10];
  107.  
  108.    while (gets(buffer) && (sscanf(buffer,"%d",x)==0))
  109.      printf("Numeric input only. Correct value /");
  110.  
  111.    return;
  112.   }
  113.  
  114.  *************************** the main program **********************
  115.  *******************************************************************/
  116.  
  117. main()
  118.   {FILE *outfile1, *outfile2;
  119.    float xlo, xhi, ylo, yhi, xdelta, ydelta, x, y, z;
  120.    float zlo=9E30, zhi=-9E30;
  121.    int xdivide, ydivide, i, j, point=1;
  122.    char buffer[40];
  123.  
  124.    /***************************************************************
  125.     ***************************************************************
  126.     Give the range of x and y values to plot, and the stepsize between
  127.     points.
  128.     ***************************************************************
  129.     ****************************************************************/
  130.  
  131.    printf("xlo /"); readreal(&xlo);
  132.    printf("xhi /"); readreal(&xhi);
  133.    printf("# of X subdivisions /"); readint(&xdivide);
  134.    xdelta = (xhi-xlo) / xdivide;
  135.  
  136.    printf("\nylo /"); readreal(&ylo);
  137.    printf("yhi /"); readreal(&yhi);
  138.    printf("# of Y subdivisions /"); readint(&ydivide);
  139.    ydelta = (yhi-ylo) / ydivide;
  140.  
  141. /*
  142. Date: 22 Sep 91 09:42:18 EDT
  143. From: Bruce Colletti <71121.1452@CompuServe.COM>
  144. Subject: 3D Viewer Part 2
  145. To: Oscar Garcia <garciao@MOF.GOVT.NZ>
  146. Message-Id: <910922134217_71121.1452_DHJ35-2@CompuServe.COM>
  147. */
  148.  
  149.    /*****************************************************************
  150.     *****************************************************************
  151.     Two files are created for later joining.  The user-specified file
  152.     outfile1 will have the (x,y,z) points to plot.  ###.### identifies
  153.     which points to connect.  The format and reason for this
  154.     approach are found in Oscar's 3DV.DOC.
  155.  
  156.     Starting from the lower left hand corner of the XY range (X
  157.     horizontal, Y vertical), process horizontal grid rows in-turn.
  158.     *****************************************************************
  159.     *****************************************************************/
  160.  
  161.    printf("Output file (.3d extension!) /");
  162.    gets(buffer);
  163.    outfile1 = fopen(buffer,"wt");
  164.    outfile2 = fopen("###.###","wt");
  165.    fprintf(outfile1,"%d\n",(xdivide+1)*(ydivide+1)+6);
  166.                    /* total # of points; +6 is to draw the axes */
  167.  
  168.    fprintf(outfile2,"%d\n",(xdivide+1)*(ydivide+1)+
  169.                            (ydivide+1)*(xdivide+1)+6);
  170.                   /* total # of connections+moves; +6 for axes */
  171.    y = ylo;
  172.  
  173.    for(i=0; i<=ydivide; i++)
  174.      {x = xlo;              /* start of horizontal grid row */
  175.       z = f(x,y);
  176.       if (z < zlo) zlo = z;
  177.       if (z > zhi) zhi = z;
  178.  
  179.       fprintf(outfile1,"%11.6f %11.6f %11.6f\n",x,y,z);
  180.                                   /* print (x,y,z) point */
  181.       fprintf(outfile2,"%d 0\n",point++); /* move to point */
  182.  
  183.       for(j=0; j<xdivide; j++)
  184.         {x += xdelta;              /* move to next point on row */
  185.          z = f(x,y);
  186.          if (z < zlo) zlo = z;
  187.          if (z > zhi) zhi = z;
  188.          fprintf(outfile1,"%11.6f %11.6f %11.6f\n",x,y,z);
  189.                                    /* print (x,y,z) point */
  190.          fprintf(outfile2,"%d 14\n",point++);
  191.                                    /* connect to previous point */
  192.         }
  193.  
  194.       y += ydelta;                 /* move to next row */
  195.      }
  196.  
  197.    /************************************************************
  198.     ************************************************************
  199.     Establish points whose pairwise connections will be the x,y,z
  200.     axes.
  201.     ********************             fprintf(outfile1,"%11.6f 0 0\n",xlo);
  202.  
  203.    /* y axis */  fprintf(outfile1,"0 %11.6f 0\n",yhi);
  204.                  fprintf(outfile1,"0 %11.6f 0\n",ylo);
  205.  
  206.    /* z axis */  fprintf(outfile1,"0 0 %11.6f\n",zhi);
  207.                  fprintf(outfile1,"0 0 %11.6f\n",zlo);
  208.  
  209.    /* now draw the axes:  the red (x), white (y), and blue (z)! */
  210.     fprintf(outfile2,"%d 0\n",point++);
  211.     fprintf(outfil2,"%d 12\n",point++);
  212.  
  213.     fprintf(outfile2,"%d 0\n",point++);
  214.     fprintf(outfile2,"%d 15\n",point++);
  215.  
  216.     fprintf(outfile2,"%d 0\n",point++);
  217.     fprintf(outfile2,"%d  9\n",point);
  218.  
  219.    /**********************************************
  220.     ***************************************************************
  221.  points have been computed and each row has been
  222.     internally conected.  Append the contents of outfile2 to outfile1
  223.     ***************************************************************
  224.     ****************************************************************/
  225.  
  226.     fclose(outfile2);
  227.     outfile2 = fopen("###.###","rt");
  228.  
  229.     while (fgets(buffer,80,outfile2) != NULL)
  230. tf(outfile1,"%s",buffer);
  231.  
  232. se ###.###");
  233.  
  234.    /*********************************
  235.     *************************************************************
  236.               Internally connect the grid's vertical columns.
  237.     ************************************************************
  238.     ************************************************************/
  239.  
  240.     for(i=1; i <= xdivide+1; i++)
  241.       {point = i;
  242.        fprintf(outfile1,"%d 0\n",point);
  243.  
  244.        for(j=0; j < ydivide; j++)
  245.         {point += xdivide+1;
  246.          fprintf(outfile1,"%d 14\n",point);
  247.         }
  248.       }
  249.  
  250.     fclose(outfile1);
  251.     printf("All Done!");
  252.    }
  253.